@{
    Layout = null;
}

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>添加外协发料单头</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="~/libs/scripts/layui-v2.5.6/layui/css/layui.css" />
    <style>
        body { padding: 20px; background: #fff; }
        .layui-form { max-width: 100%; }
        .layui-form-item { margin-bottom: 15px; }
        .required { color: red; }
    </style>
</head>
<body>
    <form class="layui-form" id="addForm" lay-filter="addForm">
        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>发料单编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="issueListCode" placeholder="系统自动生成" class="layui-input" readonly>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label"><span class="required">*</span>发料单名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="issueListName" placeholder="请输入发料单名称" class="layui-input" lay-verify="required">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">发料日期</label>
                    <div class="layui-input-block">
                        <input type="text" name="issueDate" placeholder="请选择发料日期" class="layui-input" id="issueDate">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">发料仓库</label>
                    <div class="layui-input-block">
                        <select name="issueWarehouse" lay-filter="issueWarehouse" lay-search>
                            <option value="">请选择</option>
                            <option value="仓库1">仓库1</option>
                            <option value="仓库2">仓库2</option>
                            <option value="仓库3">仓库3</option>
                        </select>
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">单据状态</label>
                    <div class="layui-input-block">
                        <select name="state" lay-filter="state">
                            <option value="">请选择</option>
                            <option value="0">待审核</option>
                            <option value="1" selected>已完成</option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">外协工单</label>
                    <div class="layui-input-block">
                        <input type="text" name="outsourcedOrders" placeholder="请输入外协工单编号" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-row layui-col-space16">
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">供应商编码</label>
                    <div class="layui-input-block">
                        <input type="text" name="vendor" placeholder="请输入供应商编码" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-col-md6">
                <div class="layui-form-item">
                    <label class="layui-form-label">供应商名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="vendorName" placeholder="请输入供应商名称" class="layui-input">
                    </div>
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" class="layui-textarea" rows="3"></textarea>
            </div>
        </div>

        <div class="layui-form-item" style="text-align: center; margin-top: 30px;">
            <button type="submit" class="layui-btn" lay-submit lay-filter="submitForm">保存</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="closeDialog()">取消</button>
        </div>
    </form>

    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/scripts/layui-v2.5.6/layui/layui.js"></script>
    <script>
        layui.use(['form', 'laydate', 'layer'], function () {
            var form = layui.form;
            var laydate = layui.laydate;
            var layer = layui.layer;

            // 页面加载时自动生成发料单编号
            generateIssueListCode();

            // 日期选择器
            laydate.render({
                elem: '#issueDate',
                value: new Date()
            });

            // 生成发料单编号
            function generateIssueListCode() {
                var now = new Date();
                var code = 'FL' + now.getFullYear() + 
                          String(now.getMonth() + 1).padStart(2, '0') + 
                          String(now.getDate()).padStart(2, '0') + 
                          String(now.getHours()).padStart(2, '0') + 
                          String(now.getMinutes()).padStart(2, '0') + 
                          String(now.getSeconds()).padStart(2, '0') + 
                          String(Math.floor(Math.random() * 1000)).padStart(3, '0');
                $('input[name="issueListCode"]').val(code);
            }

            // 表单提交
            form.on('submit(submitForm)', function (data) {
                var formData = data.field;
                
                // 日期格式处理
                if (formData.issueDate) {
                    formData.issueDate = new Date(formData.issueDate).toISOString();
                }

                $.ajax({
                    url: '/Outsourcing/AddOutsourcingMaterial',
                    type: 'POST',
                    data: formData,
                    success: function (result) {
                        if (result > 0) {
                            layer.msg('保存成功', { icon: 1 }, function () {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg('保存失败', { icon: 2 });
                        }
                    },
                    error: function () {
                        layer.msg('保存失败', { icon: 2 });
                    }
                });
                return false;
            });
        });

        // 关闭弹框
        function closeDialog() {
            var index = parent.layer.getFrameIndex(window.name);
            parent.layer.close(index);
        }
    </script>
</body>
</html> 